/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elevatronic;

import elevatronic.models.Building;
import elevatronic.view.MainFrame;
import elevatronic.view.right.parameter.SpeedSliderDelayView;
import elevatronic.view.right.parameter.SpeedSliderStepView;

/**
 *
 * @author Exosta
 */
public class Elevatronic {
	//Permit to access easiest the Main class
	private static Elevatronic singleton;	
	synchronized public static Elevatronic getSingleton() {
		return singleton;
	}
	synchronized public static void setSingleton(Elevatronic singleton) {
		Elevatronic.singleton = singleton;
	}

	//MainFrame is the main class used to manage the view
	private MainFrame mainFrame;
	public MainFrame getMainFrame() {
		return mainFrame;
	}
	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	//step of the iteration
	private int step=-1;	
	synchronized public int getStep() {
		if (step==-1){
			step=SpeedSliderStepView.convert(SpeedSliderStepView.INIT);
		}
		return step;
	}
	synchronized public void setStep(int step) {
		this.step = step;
	}
	
	//time to wait between each cycle
	private int delayTime=-1;
	synchronized public int getDelayTime() {
		if (delayTime==-1){
			delayTime=SpeedSliderDelayView.convert(SpeedSliderDelayView.INIT);
		}
		return delayTime;
	}
	synchronized public void setDelayTime(int delayTime) {
		this.delayTime = delayTime;
	}
	
	
	//the model of the building, contain the main value about the building
	private Building building;
	public Building getBuilding() {
		return building;
	}
	public void setBuilding(Building building) {
		this.building = building;
	}

	/**
     * @param args the command line arguments
	 * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
    	singleton=new Elevatronic();
    }
    
    public Elevatronic() throws InterruptedException{
    	setSingleton(this);
    	
    	//instantiate the model building
    	building=new Building();
    	
    	//instantiate the view
    	mainFrame=new MainFrame(building);
    	
    	//<the iteration of the application>
    	while (true) {
			try {
				Thread.sleep(getDelayTime());
				//update the view
				mainFrame.update(getStep());
				
				//update every models
				getBuilding().update(getStep());
			}catch (NullPointerException e) {
				e.printStackTrace();
			}
		}
    	//</the iteration of the application>
    }
    
}
